#include "ValueProcessor.h"
#include <QDebug>

ValueProcessor::ValueProcessor(QObject *parent)
    : QObject(parent)
{
    connect(&timer, &QTimer::timeout, this, &ValueProcessor::onTimeout);
}

void ValueProcessor::addValue(int a)
{
    splitAndEnqueue(a);
}

void ValueProcessor::setInterval(int intervalMs)
{
    timer.setInterval(intervalMs);
}

void ValueProcessor::startProcessing()
{
    if (!timer.isActive()) {
        timer.start();
    }
}

void ValueProcessor::splitAndEnqueue(int a)
{
    int temp = 100;
    int val = a;

    if(val < 0){
        val = -val;
    }

    int quotient = val / temp;
    int remainder = val % temp;

    for (int i = 0; i < quotient; ++i) {
        queue.enqueue(temp);
    }
    if (remainder > 0) {
        queue.enqueue(remainder);
    }
}

void ValueProcessor::onTimeout()
{
    if (!queue.isEmpty()) {
        int value = queue.dequeue();
        qDebug() << QString("Processing value: %1  Size: %2").arg(value).arg(queue.count());
    } else {
        qDebug() << "Queue is empty, stopping timer.";
        timer.stop();
    }
}
